import { Controller, Post, Body, HttpStatus } from '@nestjs/common';
import { UserService } from './user.service';
import { RegisterUserDto } from './dto/register-user.dto';
import { LoginUserDto } from './dto/login-user.dto';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { Logger } from 'src/common/logger/logger';

@ApiTags('用户管理')
@Controller('user')
export class UserController {
  constructor(
    private readonly userService: UserService,
    private readonly logger: Logger,
  ) {}

  @ApiOperation({
    summary: '用户注册',
    description: '用户注册接口，用于注册新用户',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '用户注册成功',
    type: RegisterUserDto,
  })
  @Post('register')
  register(@Body() registerUserDto: RegisterUserDto) {
    this.logger.log(JSON.stringify(registerUserDto), '333');
    return this.userService.register(registerUserDto);
  }

  @ApiOperation({
    summary: '用户登陆',
    description: '用户登陆',
  })
  @ApiResponse({
    status: 200,
    description: '成功登陆',
    type: LoginUserDto,
  })
  @ApiResponse({
    status: HttpStatus.UNAUTHORIZED,
    description: '用户名或密码错误',
  })
  @Post('login')
  async login(@Body() loginUserDto: LoginUserDto) {
    try {
      return this.userService.login(loginUserDto);
    } catch (error) {
      console.error('Login error:', error);
      throw error;
    }
  }
}
